//
//  SignInViewController.m
//  PlayCatchGame
//
//  Created by 流言 on 2018/1/25.
//  Copyright © 2018年 清正. All rights reserved.
//

#import "SignInViewController.h"
#import "SingInItemView.h"
#import "AppHeader.h"

@interface SignInViewController () {
    
    __weak IBOutlet NSLayoutConstraint *contentView_topSpace;
    __weak IBOutlet NSLayoutConstraint *contentView_height;
    __weak IBOutlet NSLayoutConstraint *contentView_width;
    
    NSArray *titleArray;
    NSMutableArray *itemList;
}
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIButton *singinButton;

@end

@implementation SignInViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    itemList = [NSMutableArray array];
    titleArray =@[@"第一天", @"第二天", @"第三天", @"第四天", @"第五天", @"第六天", @"第七天"];
    NSInteger rowCount =3;
    CGFloat itemSpace =20.0;
    
    if (IS_IPHONE_X) {
        contentView_topSpace.constant =230;
    } else if (IS_IPHONE_6PLUS) {
        contentView_topSpace.constant =220;
    } else if (IS_IPHONE_6) {
        contentView_topSpace.constant =190;
    } else if (IS_IPHONE_5) {
        contentView_topSpace.constant =160;
        contentView_height.constant =310;
    }
    CGFloat itemWidth =(CGRectGetWidth(self.contentView.frame)-itemSpace*(rowCount-1)) / (rowCount*1.0);
    CGFloat itemHeight =itemWidth*1.2;
    for (NSInteger i =0; i<titleArray.count; i++) {
        SingInItemView *itemView =[[[NSBundle mainBundle]loadNibNamed:@"SingInItemView" owner:nil options:nil] firstObject];
        itemView.titleLabel.text = titleArray[i];
        itemView.moreDay =i+1;
        if (i ==0) {
            itemView.isSiginType =YES;
        }
        [self.contentView addSubview:itemView];
        [itemList addObject:itemView];
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i ==titleArray.count-1) {
                make.top.mas_equalTo(self.contentView.mas_top).offset(i/rowCount*(5+itemHeight));
                make.centerX.mas_equalTo(self.contentView.mas_centerX).offset(0);
                make.width.mas_equalTo(itemWidth+15);
                make.height.mas_equalTo(itemHeight);
            } else {
                make.top.mas_equalTo(self.contentView.mas_top).offset(i/rowCount*(5+itemHeight));
                make.left.mas_equalTo(self.contentView.mas_left).offset(i%rowCount*(itemWidth+itemSpace));
                make.width.mas_equalTo(itemWidth);
                make.height.mas_equalTo(itemHeight);
            }
        }];
    }
    [self _signRecord];
    [self _isSigned];
}

- (void)_signRecord{
    [HttpUrl GET:@"signRecord/signNumber" dict:nil hud:self.view isShow:YES WithSuccessBlock:^(id data) {
        if (BB_isSuccess) {
            NSInteger signInDay = [[data[@"data"] description] integerValue];
            for (int i = 0; i < itemList.count ; i++) {
                SingInItemView *view = itemList[i];
                if (i < signInDay) {
                    view.isSiginType = YES;
                } else {
                    view.isSiginType = NO;
                }
            }
        } else {
            ShowAlertTitle(@"获取签到信息失败");
        }
    }];
}

- (void)_isSigned {
    [HttpUrl POST:@"signRecord/confirmSignRecord" dict:nil hud:self.view isShow:YES WithSuccessBlock:^(id data) {
        if (BB_isSuccess) {
            BOOL isSigned = [data[@"data"] boolValue];
            if (isSigned) {
                [self.singinButton setTitle:@"已签到" forState:UIControlStateNormal];
                self.singinButton.userInteractionEnabled = NO;
            }
        } else {
            
        }
    }];
}


- (IBAction)singinButtonClick:(UIButton *)sender {
    [HttpUrl GET:@"signRecord/addSign" dict:nil hud:self.view isShow:YES WithSuccessBlock:^(id data) {
        if (BB_isSuccess) {
            jxt_showToastTitle(@"签到成功", 2);
            [self _signRecord];
        } else {
            jxt_showToastTitle(@"已签到", 2);
            [self.singinButton setTitle:@"已签到" forState:UIControlStateNormal];
            self.singinButton.userInteractionEnabled = NO;
        }
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
